<?php

namespace app\admin\controller;

use app\admin\model\TrainChild;
use app\admin\model\TrainList;
use app\admin\model\UserList;
use app\admin\model\UserWeb;
use app\admin\model\VideoLevel;
use app\admin\model\VideoList;
use app\admin\service\TrainService;
use think\facade\Request;

class Train extends Base
{
    protected $middleware = ['AdminCheck', 'AdminPermission'];

    // 列表
    public function task()
    {
        if (Request::isAjax()) {
            return $this->getJson(TrainList::getList());
        }
        return $this->fetch();
    }

    // 添加
    public function addTask()
    {
        if (Request::isAjax()) {
            return $this->getJson(TrainService::goAddTask());
        }
        return $this->fetch();
    }

    // 编辑
    public function editTask($id)
    {
        if (Request::isAjax()) {
            return $this->getJson(TrainService::goEditTask());
        }
        $data = TrainList::find($id);
        $data['begin_time'] = date('Y-m-d', $data['begin_time']);
        $data['end_time'] = date('Y-m-d', $data['end_time']);
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function statusTask()
    {
        return $this->getJson(TrainService::goStatusTask());
    }

    // 删除
    public function removeTask()
    {
        return $this->getJson(TrainService::goRemoveTask());
    }

    public function trainList($id)
    {
        if (Request::isAjax()) {
            return $this->getJson(TrainChild::getList($id));
        }
        $this->assign('id', $id);
        return $this->fetch();
    }

    public function addTrain($id)
    {
        if (Request::isAjax()) {
            return $this->getJson(TrainService::goAddTrain());
        }
        $this->assign('videoList', VideoList::getTrainVideoList());
        $this->assign('train_id', $id);
        return $this->fetch();
    }

    public function editTrain($id, $cid)
    {
        if (Request::isAjax()) {
            return $this->getJson(TrainService::goEditTrain());
        }
        $data = TrainChild::find($cid);
        $this->assign('data', $data);
        $this->assign('videoList', VideoList::getTrainVideoList());
        $this->assign('train_id', $id);
        return $this->fetch();
    }

    public function searchUser()
    {
        return $this->getJson(UserWeb::getSearchUser());
    }
}